<?php

class Generator_Action_File extends Generator_Action_Default 
{
    
    /**
    * User error message. 
    * 
    * @param string
    */
    protected $_message_error = 'There is an error occured while file uploading';
    
    private $_rows_number;
    
    /**
    * Tracklist values
    * 
    * @var array
    */
    private $_values = array();
    
    /**
    * Initialize form elements.
    * 
    */
    protected function _initElements() 
    {
        parent::_initElements();
        
        $this->_setAsRequired('tracklist');
    }
    
    protected function _insert() 
    {
        $this->_model()->setValues($this->_values, 
            $this->_rows_number, $this->_session_key);
    }
    
    /**
    * Gather track list
    * 
    * @param Kernel_Form_Value_File $file
    */
    public function file(Kernel_Form_Value_File $file) 
    {
        // search for tracklist rows and split them into perfomer anf title
        $number = 0;
        $file_contents = file($file->getPath());
        
        while($row = array_shift($file_contents)) {
            if ($this->_hasSeparator($row)) {
                $number ++;
                $this->_values = 
                    array_merge($this->_values, $this->_split($row, $number));
            }
        }
        
        // this is number of tracks 
        $this->_rows_number = $number;
    }
    
    /**
    * Split row by separator (" - " character by default)
    * 
    * @param string $row
    * @param integer $number
    * @return array
    */
    private function _split($row, $number) 
    {
        list($perfomer, $title) = 
            array_map('trim', explode($this->_separator(), $row));
        
        //try to pick out perfomer without any garbage like number of track 
        preg_match('|^\d*\.?\s*([\S ]+)$|is', $perfomer, $matches);
        if (isset($matches[1])) {
            $perfomer = $matches[1];
        }
        
        // in case of unknown track ("ID" string) also apply it's to the track 
        // title and bring it to the upper case
        if (0 === strcasecmp('ID', $perfomer)) {
            $perfomer = $title = 'ID';
        }
        
        // get rid of quotes and replace brackets
        $title = strtr(trim($title, '"\''), 
            array
            (
                '[' => '(', 
                ']' => ')', 
                '{' => '(', 
                '}' => ')', 
            ));
        
        
        return array
        (
            'perfomer[' . $number . ']' => $perfomer, 
            'title[' . $number . ']' => $title, 
        );
    }
    
    /**
    * Return whether given string contains separator
    * 
    * @param string $row
    * @return boolean
    */
    private function _hasSeparator($row) 
    {
        return false !== strpos($row, $this->_separator());
    }
    
    /**
    * Return separator
    * 
    * @return string
    */
    private function _separator() 
    {
        static $separator;
        
        if (is_null($separator)) {
            // do I need somehow check for user separator?
            //if (!strlen($separator = $this->_getValue('separator'))) {
                $separator = Kernel_Config::get('generator', 
                    'default_perfomer_title_separator');
            //}
        }
        
        return $separator;
    }
    
    /**
    * Return wether given string is tracklist row
    * 
    * @param string $row
    * @return boolean
    */
   /* private function _isPerformerTitleRow($row) 
    {
        return 
            !$this->_isWhiteSpace($row) &&
            !$this->_hasDate($row);
    }*/
    
    
    
    /**
    * Return whether row holds date in the format xx-xx-xxxx or xxxx-xx-xx 
    * wrapped with "()" or without
    * 
    * @param string $row
    * @return boolean
    */
    /*private function _hasDate($row) 
    {
        $pattern = '|^.* \(?(\d{2,4}-\d{2}-\d{2,4})\)?.*$|is';
        return (bool)preg_match($pattern, $row);
    }*/
    
    /**
    * Return whether given row holds only white space
    * 
    * @param string $row
    * @return boolean
    */
    /*private function _isWhiteSpace($row) 
    {
        $pattern = '/^\s+$/';
        return (bool)preg_match($pattern, $row);
    }*/
    
    /*private function _hasTime() 
    {
        $pattern = '|^(\d{2}:\d{2}) .*$|is';
    }*/
    
}